//!{====================================================================================
//! @file solveSquare.c
//! @date    2013-09-26 16:30
//! @author Andrianov Georgiy <egor.andrianov81@gmail.com>
//!
//! Solution for quadratic equations
//!
//! @par The programme enters 3 coefficients.
//!      It needs to output number of roots
//!}====================================================================================

#include <stdio.h>
#include <math.h>
#include <assert.h>

const double EPS = 0.0000000001;

#define IS_ZERO(x) fabs(x) < EPS

int SolveSquare ( double a, double b, double c, double *x1, double *x2 );

int main()
{
        double  a = 0,
                b = 0,
                c = 0;
        printf ( "a * x^2 + b * x + c = 0,  enter a, b, c \n" );
        scanf ( "%lf %lf %lf", &a, &b, &c ) ;
        double x1 = 0,
               x2 = 0;
        int nRoots = SolveSquare ( a, b, c, &x1, &x2 );
        if ( nRoots == 2 )
        {
            printf ( "nRoots = %i, x1=%lf, x2=%lf\n", nRoots, x1, x2 );
        }
        if ( nRoots == 1 )
        {
            printf ( "nRoots = %i, x = %lf", nRoots, x1 );
        }
        if ( nRoots == -1 )
        {
            printf ( "No answers\n" );
        }
        if ( nRoots == -2 )
        {
            printf ( "Infinity answers\n" );
        }
        return 0;
}

//!{============================================================================
//! SolveSquare - solve a square or linear equation specified by its coefficients.
//!
//! @param      a   Equation a-coefficient
//! @param      b   Equation b-coefficient
//! @param      c   Equation c-coefficient
//! @param[out] x1  1st root of equation, if exist (if not, value will be unchanged)
//! @param[out] x2  2nd root of equation, if exist (if not, value will be unchanged)
//!
//! @return         Number of roots or zero if none
//!}===================================================================================

int SolveSquare ( double a, double b, double c, double *x1, double *x2 )
{
        assert ( x2 != NULL );
        assert ( x1 != NULL );
        assert ( x1 != x2   );

        if ( a == 0 && b == 0 && c == 0)
        {
            return -2;
        }
        if ( a == 0 )
        {
                *x1 = -c / b;
                return 1;
        }
        else
        {
                double d = b * b - 4 * a * c;
                if ( IS_ZERO(d) )
                {
                    *x1 = -b / ( 2 * a );
                    return 1;
                }
                else
                {
                    if ( d > EPS )
                    {
                        *x1 = ( -b + sqrt(d) ) / ( 2 * a );
                        *x2 = ( -b - sqrt(d) ) / ( 2 * a );
                        return 2;
                    }
                    else
                    {
                        return -1;
                    }
                }
        }
}


